import { defineStore } from "pinia";
import { ref } from "vue";
import { constantRoutes } from "@/router/routes/constantRoutes.ts";

const useMenuStore = defineStore("menuStore", () => {
  const routes = ref(constantRoutes);
  const filterAsyncRoutes = (asyncRoutes:any, userRoutes: any[]) => {
    const resultRoutes: any[] = asyncRoutes.filter((item:any) => {
      if (userRoutes.includes(item.name)) {
        if (item.children && item.children.length !== 0) {
          item.children = filterAsyncRoutes(item.children, userRoutes);
        }
        return true;
      }
    });
    routes.value = [...resultRoutes, ...constantRoutes];
    return resultRoutes;
  };
  return {
    routes,
    filterAsyncRoutes,
  };
});

export default useMenuStore;
